package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.oss.OssUtils;
import com.ruoyi.system.domain.Student;
import com.ruoyi.system.domain.StudentProjectDescription;
import com.ruoyi.system.domain.StudentImage;
import com.ruoyi.system.service.IStudentService;
import com.ruoyi.system.service.IStudentProjectDescriptionService;
import com.ruoyi.system.service.IStudentImageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学生信息Controller
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/system/student")
public class StudentController extends BaseController
{
    @Autowired
    private IStudentService studentService;

    @Autowired
    private IStudentProjectDescriptionService studentProjectDescriptionService;

    @Autowired
    private IStudentImageService studentImageService;

    /**
     * 查询学生信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:student:list')")
    @GetMapping("/list")
    public TableDataInfo list(Student student)
    {
        startPage();
        List<Student> list = studentService.selectStudentList(student);
        return getDataTable(list);
    }

    /**
     * 导出学生信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:student:export')")
    @Log(title = "学生信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Student student)
    {
        List<Student> list = studentService.selectStudentList(student);
        ExcelUtil<Student> util = new ExcelUtil<Student>(Student.class);
        util.exportExcel(response, list, "学生信息数据");
    }

    /**
     * 获取学生信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:student:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(studentService.selectStudentById(id));
    }

    /**
     * 新增学生信息
     */
    @PreAuthorize("@ss.hasPermi('system:student:add')")
    @Log(title = "学生信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Student student)
    {
        return toAjax(studentService.insertStudent(student));
    }

    /**
     * 修改学生信息
     */
    @PreAuthorize("@ss.hasPermi('system:student:edit')")
    @Log(title = "学生信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Student student)
    {
        return toAjax(studentService.updateStudent(student));
    }

    /**
     * 删除学生信息
     */
    @PreAuthorize("@ss.hasPermi('system:student:remove')")
    @Log(title = "学生信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(studentService.deleteStudentByIds(ids));
    }

    /**
     * 获取学生信息导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Student> util = new ExcelUtil<Student>(Student.class);
        util.importTemplateExcel(response, "学生数据");
    }

    /**
     * 学生数据导入
     */
    @PreAuthorize("@ss.hasPermi('system:student:import')")
    @Log(title = "学生信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Student> util = new ExcelUtil<Student>(Student.class);
        List<Student> studentList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = studentService.importStudent(studentList, updateSupport, operName);
        return success(message);
    }

    /**
     * 上传图片
     */
    @PostMapping("/uploadImage")
    public AjaxResult uploadImage(MultipartFile file)
    {
        try
        {
            String url = OssUtils.uploadImage(file);
            return AjaxResult.success("上传成功", url);
        }
        catch (Exception e)
        {
            return error("上传失败：" + e.getMessage());
        }
    }

    /**
     * 上传视频
     */
    @PostMapping("/uploadVideo")
    public AjaxResult uploadVideo(MultipartFile file)
    {
        try
        {
            String url = OssUtils.uploadVideo(file);
            return AjaxResult.success("上传成功", url);
        }
        catch (Exception e)
        {
            return error("上传失败：" + e.getMessage());
        }
    }

    /**
     * 获取学生项目描述
     */
    @GetMapping("/description/{studentId}")
    public AjaxResult getStudentDescription(@PathVariable("studentId") Long studentId)
    {
        StudentProjectDescription description = studentProjectDescriptionService.selectStudentProjectDescriptionByStudentId(studentId);
        return success(description);
    }

    /**
     * 保存学生项目描述
     */
    @PostMapping("/description")
    public AjaxResult saveStudentDescription(@RequestBody StudentProjectDescription description)
    {
        try
        {
            // 检查是否已存在该学生的描述
            StudentProjectDescription existing = studentProjectDescriptionService.selectStudentProjectDescriptionByStudentId(description.getStudentId());
            
            if (existing != null)
            {
                // 更新现有描述
                description.setId(existing.getId());
                description.setUpdateBy(getUsername());
                studentProjectDescriptionService.updateStudentProjectDescription(description);
            }
            else
            {
                // 新增描述
                description.setCreateBy(getUsername());
                studentProjectDescriptionService.insertStudentProjectDescription(description);
            }
            
            return success("保存成功");
        }
        catch (Exception e)
        {
            return error("保存失败：" + e.getMessage());
        }
    }

    /**
     * 获取学生图片列表
     */
    @GetMapping("/images/{studentId}")
    public AjaxResult getStudentImages(@PathVariable("studentId") Long studentId)
    {
        List<StudentImage> images = studentImageService.selectStudentImagesByStudentId(studentId);
        return success(images);
    }

    /**
     * 添加学生图片
     */
    @PostMapping("/images")
    public AjaxResult addStudentImage(@RequestBody StudentImage studentImage)
    {
        try
        {
            studentImage.setCreateBy(getUsername());
            studentImageService.insertStudentImage(studentImage);
            return success("添加成功");
        }
        catch (Exception e)
        {
            return error("添加失败：" + e.getMessage());
        }
    }

    /**
     * 删除学生图片
     */
    @DeleteMapping("/images/{id}")
    public AjaxResult deleteStudentImage(@PathVariable("id") Long id)
    {
        try
        {
            studentImageService.deleteStudentImageById(id);
            return success("删除成功");
        }
        catch (Exception e)
        {
            return error("删除失败：" + e.getMessage());
        }
    }

    /**
     * 批量删除学生图片
     */
    @DeleteMapping("/images/batch")
    public AjaxResult deleteStudentImages(@RequestBody Long[] ids)
    {
        try
        {
            studentImageService.deleteStudentImageByIds(ids);
            return success("删除成功");
        }
        catch (Exception e)
        {
            return error("删除失败：" + e.getMessage());
        }
    }
}
